package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.*;
import cn.cdutetc.library.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 统计控制器
 */
@RestController
@RequestMapping("/api/statistics")
@Tag(name = "统计管理", description = "统计报表相关接口")
public class StatisticsController {
    
    @Autowired
    private StatisticsService statisticsService;
    
    @GetMapping("/dashboard")
    @Operation(summary = "获取仪表板统计数据", description = "获取仪表板的所有统计数据")
    public ApiResponse<DashboardStatsDTO> getDashboardStats(
            @Parameter(description = "开始日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        DashboardStatsDTO stats = statisticsService.getDashboardStats(startDate, endDate);
        return ApiResponse.success(stats);
    }
    
    @GetMapping("/financial")
    @Operation(summary = "获取财务统计数据", description = "获取财务相关的统计数据")
    public ApiResponse<FinancialStatsDTO> getFinancialStats(
            @Parameter(description = "开始日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        FinancialStatsDTO stats = statisticsService.getFinancialStats(startDate, endDate);
        return ApiResponse.success(stats);
    }
    
    @GetMapping("/borrow-trend")
    @Operation(summary = "获取借阅趋势", description = "获取指定日期范围的借阅趋势数据")
    public ApiResponse<List<ChartDataDTO>> getBorrowTrend(
            @Parameter(description = "开始日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @Parameter(description = "天数（当日期范围未指定时使用）") @RequestParam(required = false) Integer days) {
        List<ChartDataDTO> trend = statisticsService.getBorrowTrend(startDate, endDate, days);
        return ApiResponse.success(trend);
    }
    
    @GetMapping("/category-stats")
    @Operation(summary = "获取图书分类统计", description = "获取图书分类统计数据")
    public ApiResponse<List<ChartDataDTO>> getCategoryStats() {
        List<ChartDataDTO> stats = statisticsService.getCategoryStats();
        return ApiResponse.success(stats);
    }
    
    @GetMapping("/financial-trend")
    @Operation(summary = "获取财务趋势", description = "获取指定日期范围的财务趋势数据")
    public ApiResponse<List<ChartDataDTO>> getFinancialTrend(
            @Parameter(description = "开始日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @Parameter(description = "天数（当日期范围未指定时使用）") @RequestParam(required = false) Integer days) {
        List<ChartDataDTO> trend = statisticsService.getFinancialTrend(startDate, endDate, days);
        return ApiResponse.success(trend);
    }
    
    @GetMapping("/popular-books")
    @Operation(summary = "获取热门图书排行", description = "获取借阅次数最多的图书排行")
    public ApiResponse<List<BookRankDTO>> getPopularBooks(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<BookRankDTO> books = statisticsService.getPopularBooks(limit);
        return ApiResponse.success(books);
    }
    
    @GetMapping("/active-users")
    @Operation(summary = "获取活跃用户排行", description = "获取借阅次数最多的用户排行")
    public ApiResponse<List<UserRankDTO>> getActiveUsers(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<UserRankDTO> users = statisticsService.getActiveUsers(limit);
        return ApiResponse.success(users);
    }
    
    @GetMapping("/borrow-status-stats")
    @Operation(summary = "获取借阅状态分布", description = "获取借阅状态分布统计数据")
    public ApiResponse<List<ChartDataDTO>> getBorrowStatusStats() {
        List<ChartDataDTO> stats = statisticsService.getBorrowStatusStats();
        return ApiResponse.success(stats);
    }
    
    @GetMapping("/user-role-stats")
    @Operation(summary = "获取用户角色分布", description = "获取用户角色分布统计数据")
    public ApiResponse<List<ChartDataDTO>> getUserRoleStats() {
        List<ChartDataDTO> stats = statisticsService.getUserRoleStats();
        return ApiResponse.success(stats);
    }
    
    @GetMapping("/new-users-count")
    @Operation(summary = "获取新增用户数量", description = "获取最近7天新增用户数量")
    public ApiResponse<Long> getNewUsersCount() {
        Long count = statisticsService.getNewUsersCount();
        return ApiResponse.success(count);
    }
    
    @GetMapping("/active-users-count")
    @Operation(summary = "获取活跃用户数量", description = "获取最近7天有借阅记录的活跃用户数量")
    public ApiResponse<Long> getActiveUsersCount() {
        Long count = statisticsService.getActiveUsersCount();
        return ApiResponse.success(count);
    }
}
